﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using SDTVKVV;
using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    public partial class SDTVKVVData : BaseFunction
    {
        public string ITXMANDANT = "00", ITXDBVERSION = "50.24", ITXUSER = "INTEXMAP", ITXDATE = "20120723";
        public string ITXLOOMTYPE = "T610  ";
        public string ITXFART = "                    ", ITXGART = "                    ", ITXBART = "                    ";
        public string ITXFVAR = "  ", ITXGVAR = "  ", ITXBVAR = "1 ";
        public string ITXFCOLOR = "          ", ITXGCOLOR = "          ", ITXBCOLOR = "          ";
        public string ITXFFINISH = "          ", ITXCADID = "1";
        public string ITXWBLO = "TX", ITXWBLB = "01";

        public string CADCHAR = " ", ITXCHAR = " ";
        DataRow dwIntexStd = null;
        public Dictionary<string, object> CADTable = new Dictionary<string, object>();
        private Convert2Intex Convert2Intex;
        public DataSet SetExt;
        INTEXDBC INTEXDBC;
        public SDTVKVVData()
            : base()
        {
        }
        public SDTVKVVData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            SetExt = new DataSet();
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public SDTVKVVData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        #region=============INTEX WS/BS=============================
        public DataSet ResetSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.xls");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                }
                setResult = exAgent.LoadDataTableDef(filePath, "");
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setResult.WriteXml(filePath);
                setResult = new DataSet();
                setResult = LoadSetDispo(setID);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                setResult.ReadXml(filePath);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveSetDispo(DataTable tbDispo)
        {
            DataSet setDispo = new DataSet();

            try
            {
                setDispo.Merge(tbDispo);
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\DATADISPO.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                    setDispo.WriteXml(filePath);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        public DataSet ConvertToIntex(ref bool success, ref string failreason)
        {
            DataSet setResult = new DataSet();
            DataSet Return = new DataSet();//is used to magage the data to upload to intex
            //SetExt manage the envirement of data processing.
            DataTable tbGSCOLOR = new DataTable();
            string expression = "";
            string ITXVKKRS, ITXVKLFD;
            DataRow[] rows;
            DataRow dwVKKOPF;
            double totalwert = 0;
            try
            {
                Convert2Intex = new Convert2Intex(SetExt, dwIntexStd, OracleDB, proxy);
                dwIntexStd = Convert2Intex.dwIntexStd;
                ITXDBVERSION = dwIntexStd["ITXDBVERSION"].ToString();
                bool IsNewCustomer = false;
                setResult = GenerateKD(ref IsNewCustomer, ITXDBVERSION);
                SetExt.Merge(setResult);
                if (SetExt.Tables["KDSTAMM_ITX"].Rows.Count == 0)
                {
                    success = false;
                    return Return;
                }
                if (SetExt.Tables["KUNDVER_ITX"].Rows.Count == 0)
                {
                    success = false;
                    return Return;
                }
                //用于参与使用的表同事仅有一组记录，并且用ITX结尾
                #region=======VK==========
                ITXVKKRS = dwIntexStd["ITXVKKRS"].ToString();
                ITXVKLFD = dwIntexStd["ITXVKLFD"].ToString();
                ITXWBLO = dwIntexStd["ITXWBLO"].ToString();
                ITXWBLB = dwIntexStd["ITXWBLB"].ToString();


                dwVKKOPF = SetExt.Tables["VKKOPF_EXT"].Rows[0];
                EventDispatcher.Instance.RaiseStatus("单号生成:" + dwVKKOPF["VKKAUFKRS"].ToString() + "=" + dwVKKOPF["VKKAUFLFD"].ToString(), StatusEventType.RunningProgress);
                setResult = Convert2VKKOPF(dwVKKOPF);
                SetExt.Merge(setResult);

                setResult.Tables[0].TableName = "VKKOPF_ITX";
                if (SetExt.Tables.Contains("VKKOPF_ITX"))
                {
                    SetExt.Tables["VKKOPF_ITX"].Clear();
                }
                SetExt.Merge(setResult);

                DataRow dwVAART = null, dwVAFARBEN = null;
                DataRow dwWDECK = null;
                DataRow dwWBBST = null;
                string MANDANT, KOLL, WBBWAKZ, art, WBBDESS, WBBCOLORIT, color, finish, var;
                foreach (DataRow dwVKPOS in SetExt.Tables["VKPOS_EXT"].Rows)
                {
                    #region=====VAART_ITX==========
                    dwVAART = SetExt.Tables["VAART"].NewRow();
                    dwVAART["VAAMANDANT"] = dwVKPOS["VKPMANDANT"].ToString();
                    dwVAART["VAAKOLL"] = dwVKPOS["VKPKOLL"].ToString();
                    dwVAART["VAAART"] = dwVKPOS["VKPART"].ToString();
                    setResult = INTEXDBC.QueryVAART(dwVAART);

                    if (setResult.Tables.Count == 0)
                        continue;
                    if (setResult.Tables[0].Rows.Count == 0)
                        continue;

                    setResult.Tables[0].TableName = "VAART_ITX";
                    if (SetExt.Tables.Contains("VAART_ITX"))
                    {
                        SetExt.Tables["VAART_ITX"].Clear();
                    }
                    SetExt.Merge(setResult);
                    dwVAART = setResult.Tables[0].Rows[0];
                    EventDispatcher.Instance.RaiseStatus("品种找到:" + dwVAART["VAAKOLL"].ToString() + "-" + dwVAART["VAAART"].ToString(), StatusEventType.RunningProgress);
                    #endregion
                    #region=====VAFARBEN_ITX==========
                    dwVAFARBEN = SetExt.Tables["VAFARBEN"].NewRow();
                    dwVAFARBEN["VAFMANDANT"] = dwVKPOS["VKPMANDANT"].ToString();
                    dwVAFARBEN["VAFKOLL"] = dwVKPOS["VKPKOLL"].ToString();
                    dwVAFARBEN["VAFART"] = dwVKPOS["VKPART"].ToString();
                    dwVAFARBEN["VAFFARBE"] = dwVKPOS["VKPFARBE"].ToString();
                    setResult = INTEXDBC.QueryVAFARBEN(dwVAFARBEN);
                    if (setResult.Tables.Count == 0)
                        continue;
                    if (setResult.Tables[0].Rows.Count == 0)
                        continue;
                    dwVAFARBEN = setResult.Tables[0].Rows[0];
                    EventDispatcher.Instance.RaiseStatus("颜色找到:" + dwVAFARBEN["VAFART"].ToString() + "-" + dwVAFARBEN["VAFFARBE"].ToString(), StatusEventType.RunningProgress);
                    setResult.Tables[0].TableName = "VAFARBEN_ITX";
                    if (SetExt.Tables.Contains("VAFARBEN_ITX"))
                    {
                        SetExt.Tables["VAFARBEN_ITX"].Clear();
                    }
                    SetExt.Merge(setResult);
                    #endregion
                    #region=====VKPOS_ITX==========
                    setResult = Convert2VKPOS(dwVKPOS);
                    SetExt.Merge(setResult);
                    setResult.Tables[0].TableName = "VKPOS_ITX";
                    if (SetExt.Tables.Contains("VKPOS_ITX"))
                    {
                        SetExt.Tables["VKPOS_ITX"].Clear();
                    }
                    SetExt.Merge(setResult);
                    DataRow rwVKPOS = setResult.Tables[0].Rows[0];
                    EventDispatcher.Instance.RaiseStatus("转换订单行:" + rwVKPOS["VKPAUFPOS"].ToString(), StatusEventType.RunningProgress);
                    #endregion
                    #region=====WDECK==========
                    DataRow rowVKPOS = setResult.Tables[0].Rows[0];
                    setResult = StartWDECK(rowVKPOS);
                    SetExt.Merge(setResult);
                    if (!setResult.Tables.Contains("WDECK"))
                        continue;
                    if (setResult.Tables["WDECK"].Rows.Count == 0)
                        continue;
                    setResult.Tables[0].TableName = "WDECK_ITX";
                    if (SetExt.Tables.Contains("WDECK_ITX"))
                    {
                        SetExt.Tables["WDECK_ITX"].Clear();
                    }
                    SetExt.Merge(setResult);
                    dwWDECK = setResult.Tables[0].Rows[0];

                    EventDispatcher.Instance.RaiseStatus("产生面料需求WDECK:" + dwWDECK["WDAUFKRS"].ToString() + dwWDECK["WDAUFLFD"].ToString() + dwWDECK["WDAUFPOS"].ToString(), StatusEventType.RunningProgress);
                    #endregion

                    #region===获取布卷库存，用于产生新布卷===
                    EventDispatcher.Instance.RaiseStatus("开始剪布WBBST/WBBEW:", StatusEventType.RunningProgress);
                    setResult = AllocateWB(dwWDECK);
                    SetExt.Merge(setResult);//WBBST and WGBST
                    if (SetExt.Tables["WBBST_ITX"].Rows.Count == 0)
                        continue;
                    #endregion

                    #region=====Ship============
                    EventDispatcher.Instance.RaiseStatus("开始发货VVLIEF/VVTLIEF:", StatusEventType.RunningProgress);
                    setResult = ShipWBBEW(dwVKKOPF, dwWBBST, dwWDECK);
                    SetExt.Merge(setResult);
                    totalwert += System.Convert.ToDouble(rowVKPOS["VKPPOSWERT"].ToString());
                    #endregion
                    #region======Cut fabric========
                    EventDispatcher.Instance.RaiseStatus("开始确认剪布", StatusEventType.RunningProgress);
                    setResult = CutWBBST(dwVKKOPF, dwWDECK);
                    SetExt.Merge(setResult);
                    EventDispatcher.Instance.RaiseStatus("完成确认剪布", StatusEventType.RunningProgress);
                    #endregion
                    #region======Pack fabric========
                    EventDispatcher.Instance.RaiseStatus("开始包装", StatusEventType.RunningProgress);
                    setResult =PackWBBST(dwVKKOPF, dwWDECK);
                    SetExt.Merge(setResult);
                    EventDispatcher.Instance.RaiseStatus("完成包装", StatusEventType.RunningProgress);
                    #endregion
                }

                double VKPMENGE1T1 = 0, VKPMENGE1T2 = 0, VKPMENGE1T3 = 0, VKPMENGE1T4 = 0, VKPPOSWERT = 0;

                foreach (DataRow dwVKPOS in SetExt.Tables["VKPOS"].Rows)
                {
                    VKPMENGE1T1 += System.Convert.ToDouble(dwVKPOS["VKPMENGE1T1"].ToString());
                    VKPMENGE1T2 += System.Convert.ToDouble(dwVKPOS["VKPMENGE1T2"].ToString());
                    VKPMENGE1T3 += System.Convert.ToDouble(dwVKPOS["VKPMENGE1T3"].ToString());
                    VKPMENGE1T4 += System.Convert.ToDouble(dwVKPOS["VKPMENGE1T4"].ToString());
                    VKPPOSWERT += System.Convert.ToDouble(dwVKPOS["VKPPOSWERT"].ToString());
                }

                dwVKKOPF = SetExt.Tables["VKKOPF"].Rows[0];
                dwVKKOPF["VKKGESMENGE1"] = System.Math.Round(VKPMENGE1T1 * 0.01, 0);
                dwVKKOPF["VKKGESMENGE2"] = System.Math.Round(VKPMENGE1T2 * 0.01, 0);
                dwVKKOPF["VKKGESMENGE3"] = System.Math.Round(VKPMENGE1T3 * 0.01, 0);
                dwVKKOPF["VKKGESMENGE4"] = System.Math.Round(VKPMENGE1T4 * 0.01, 0);
                dwVKKOPF["VKKGESWERT"] = System.Math.Round(VKPPOSWERT, 0);
                double VVNETMTR = 0, VVBRUTTO = 0, VVNETTO = 0, VVLIEFSCHPOS = 0, VVANZTLIEF = 0, VVANZSTK = 0, VVVSTK = 0;
                if (SetExt.Tables.Contains("VVTLIEF"))
                {
                    foreach (DataRow row in SetExt.Tables["VVTLIEF"].Rows)
                    {
                        VVLIEFSCHPOS++;
                        VVANZTLIEF++;
                        //VVVSTK++;
                        VVANZSTK++;
                        VVBRUTTO += System.Convert.ToDouble(row["VVTBRUTTO"].ToString());
                        VVNETTO += System.Convert.ToDouble(row["VVTNETTO"].ToString());
                        VVNETMTR += System.Convert.ToDouble(row["VVTMTR"].ToString());
                    }
                    if (SetExt.Tables.Contains("VVLIEF"))
                    {
                        DataRow dwVVLIEF = SetExt.Tables["VVLIEF"].Rows[0];
                        dwVVLIEF["VVBRUTTO"] = System.Math.Round(VVBRUTTO, 0);
                        dwVVLIEF["VVNETTO"] = System.Math.Round(VVNETTO, 0);
                        dwVVLIEF["VVNETMTR"] = System.Math.Round(VVNETMTR, 0);
                        //dwVVLIEF["VVVSTK"] = System.Math.Round(VVVSTK, 0);
                        dwVVLIEF["VVANZSTK"] = System.Math.Round(VVANZSTK, 0);

                        dwVVLIEF["VVLIEFSCHPOS"] = Right("000" + VVLIEFSCHPOS, 3);
                        dwVVLIEF["VVANZTLIEF"] = System.Math.Round(VVLIEFSCHPOS, 0);
                        dwVVLIEF["VVWERT"] = Right("0000000000" + (System.Math.Round(totalwert * 1000, 0)).ToString(), 10);
                    }
                }
                #endregion

                Return.Merge(SetExt.Tables["VKPOS"]);
                Return.Merge(SetExt.Tables["VKKOPF"]);
                if (SetExt.Tables.Contains("WBBST"))
                {
                    Return.Merge(SetExt.Tables["WBBST"]);
                    Return.Merge(SetExt.Tables["WDECK"]);
                    Return.Merge(SetExt.Tables["WBBEW"]);
                    Return.Merge(SetExt.Tables["VVLIEF"]);
                    Return.Merge(SetExt.Tables["VVTLIEF"]);
                    Return.Merge(SetExt.Tables["VVPACK"]);
                    Return.Merge(SetExt.Tables["WGBST"]);
                }
                //VKPOS VKKOPF WBBST WBBEW VVLIEF VVTLIEF
            }
            catch (Exception ex)
            {
                success = false;
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
    }
}